﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CloudFranceTriple.Models;
using WebMatrix.WebData;
using System.Text;


namespace CloudFranceTriple.WebRole.Areas.Gaming.Controllers
{
    [CloudFranceTriple.WebRole.Filters.InitializeSimpleMembership]
    public class EventController : Controller
    {
        private cloudfrAboq2RsDMEntities db = new cloudfrAboq2RsDMEntities();

        private readonly IWebSecurityWrapper _webSecurity;
        public EventController()
        {
            this._webSecurity = new WebSecurityWrapper();
        }

        public EventController(IWebSecurityWrapper webSecurity)
        {
            this._webSecurity = webSecurity;
        }
        //
        // GET: /Gaming/Event/

        [AllowAnonymous()]
        [Authorize(Roles = "RoleAdminEvent")]
        public ActionResult ShowEvents(int id = 0)
        {
            var cat = db.EventCategorySet.Find(id);
            ViewBag.ColorCategory = String.Format("<div style='background-color: #{0};color: #{1};border-radius: 8px;' ><b>{2}</b></div>",
                cat.BackColorEventCategory, cat.ColorEventCategory, cat.LabelEventCategory);

            var eventSet = db.EventSet.Where(event2 => event2.EventCategory_Id == id).OrderByDescending(event1 => event1.DateStartEvent);
            return PartialView("_ShowEventsByCat", eventSet.ToList());
        }

        //
        // GET: /Gaming/Event/

        [AllowAnonymous()]
        [Authorize(Roles = "RoleAdminEvent")]
        public ActionResult Index()
        {
            var catSet = db.EventCategorySet.OrderBy(cat1 => cat1.OrderEventCategory);
            return View(catSet.ToList());
        }

        //
        // GET: /Gaming/Event/Details/5

        [AllowAnonymous()]
        [Authorize(Roles = "RoleAdminEvent")]
        public ActionResult Details(int id = 0)
        {
            EventSet eventset = db.EventSet.Find(id);
            if (eventset == null)
            {
                return HttpNotFound();
            }
            return View(eventset);
        }

        //
        // GET: /Gaming/Event/Create

        [Authorize(Roles = "RoleAdminEvent")]
        public ActionResult Create()
        {
            ViewBag.EventCategory_Id = new SelectList(db.EventCategorySet, "Id", "LabelEventCategory");
            return View(new EventSet { DateStartEvent = DateTime.UtcNow, DateEndEvent = DateTime.UtcNow.AddHours(2) });
        }

        //
        // POST: /Gaming/Event/Create

        [HttpPost]
        [ValidateAntiForgeryToken]
        [Authorize(Roles = "RoleAdminEvent")]
        [ValidateInput(false)]
        public ActionResult Create(EventSet eventset)
        {
            if (ModelState.IsValid)
            {
                eventset.AuthorEvent = User.Identity.Name;

                var startHourTime = Request.Form["DateStartEvent.HourTime"].ToString();
                var startMinuteTime = Request.Form["DateStartEvent.MinuteTime"].ToString();
                var endHourTime = Request.Form["DateEndEvent.HourTime"].ToString();
                var endMinuteTime = Request.Form["DateEndEvent.MinuteTime"].ToString();

                eventset.DateStartEvent = eventset.DateStartEvent.Date.AddHours(Int32.Parse(startHourTime)).AddMinutes(Int32.Parse(startMinuteTime));
                eventset.DateEndEvent = eventset.DateEndEvent.Date.AddHours(Int32.Parse(endHourTime)).AddMinutes(Int32.Parse(endMinuteTime));

                eventset.DateStartEvent = Rules.ConvertTimeZoneFromUserToUtc(_webSecurity.CurrentUserId, eventset.DateStartEvent);
                eventset.DateEndEvent = Rules.ConvertTimeZoneFromUserToUtc(_webSecurity.CurrentUserId, eventset.DateEndEvent);

                db.EventSet.Add(eventset);
                db.SaveChanges();
                return RedirectToAction("Index", "Event", new { area = "Gaming" });
            }
            ViewBag.EventCategory_Id = new SelectList(db.EventCategorySet, "Id", "LabelEventCategory", eventset.EventCategory_Id);
            return View(eventset);
        }

        //
        // GET: /Gaming/Event/Edit/5

        [Authorize(Roles = "RoleAdminEvent")]
        public ActionResult Edit(int id = 0)
        {
            EventSet eventset = db.EventSet.Find(id);
            if (eventset == null)
            {
                return HttpNotFound();
            }
            ViewBag.EventCategory_Id = new SelectList(db.EventCategorySet, "Id", "LabelEventCategory", eventset.EventCategory_Id);
            return View(eventset);
        }

        //
        // POST: /Gaming/Event/Edit/5

        [HttpPost]
        [ValidateAntiForgeryToken]
        [Authorize(Roles = "RoleAdminEvent")]
        [ValidateInput(false)]
        public ActionResult Edit(EventSet eventset)
        {
            if (ModelState.IsValid)
            {
                var startHourTime = Request.Form["DateStartEvent.HourTime"].ToString();
                var startMinuteTime = Request.Form["DateStartEvent.MinuteTime"].ToString();
                var endHourTime = Request.Form["DateEndEvent.HourTime"].ToString();
                var endMinuteTime = Request.Form["DateEndEvent.MinuteTime"].ToString();

                eventset.DateStartEvent = eventset.DateStartEvent.Date.AddHours(Int32.Parse(startHourTime)).AddMinutes(Int32.Parse(startMinuteTime));
                eventset.DateEndEvent = eventset.DateEndEvent.Date.AddHours(Int32.Parse(endHourTime)).AddMinutes(Int32.Parse(endMinuteTime));

                eventset.DateStartEvent = Rules.ConvertTimeZoneFromUserToUtc(_webSecurity.CurrentUserId, eventset.DateStartEvent);
                eventset.DateEndEvent = Rules.ConvertTimeZoneFromUserToUtc(_webSecurity.CurrentUserId, eventset.DateEndEvent);

                db.Entry(eventset).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index", "Event", new { area = "Gaming" });
            }
            ViewBag.EventCategory_Id = new SelectList(db.EventCategorySet, "Id", "LabelEventCategory", eventset.EventCategory_Id);
            return View(eventset);
        }

        //
        // GET: /Gaming/Event/Delete/5

        [Authorize(Roles = "RoleAdminEvent")]
        public ActionResult Delete(int id = 0)
        {
            EventSet eventset = db.EventSet.Find(id);
            if (eventset == null)
            {
                return HttpNotFound();
            }
            return View(eventset);
        }

        //
        // POST: /Gaming/Event/Delete/5

        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        [Authorize(Roles = "RoleAdminEvent")]
        public ActionResult DeleteConfirmed(int id)
        {
            EventSet eventset = db.EventSet.Find(id);
            db.EventSet.Remove(eventset);
            db.SaveChanges();
            return RedirectToAction("Index", "Event", new { area = "Gaming" });
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}